package cn.lyq.card.ui.course

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import cn.lyq.card.R
import cn.lyq.card.base.BaseFg
import cn.lyq.card.data.model.CourseInfo
import cn.lyq.card.data.model.PageInfo
import cn.lyq.card.data.src.Result
import cn.lyq.card.data.src.Status
import cn.lyq.card.ui.web.WebActivity
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.fg_refresh_rv.*


class CourseSubFg : BaseFg() {

    companion object {
        private const val KEY_INDEX = "INDEX"

        fun newInstance(Index: Int): CourseSubFg {
            val args = Bundle()
            args.putInt(KEY_INDEX, Index)
            val fragment = CourseSubFg()
            fragment.arguments = args
            return fragment
        }
    }

    private val mIndex by lazy {
        arguments?.getInt(KEY_INDEX, 1) ?: 1
    }
    private val mModel by lazy {
        getVMActivity(CourseVM::class.java)
    }
    private val mAdapter by lazy {
        CourseAdapter()
    }
    private val page = PageInfo()

    override fun getLayoutId(): Int {
        return R.layout.fg_refresh_rv
    }

    override fun initView(view: View) {
        super.initView(view)
        rv.layoutManager = LinearLayoutManager(requireContext())
        rv.adapter = mAdapter
        mAdapter.setEmptyView(R.layout.layout_empty)
        mAdapter.loadMoreModule.apply {
            checkDisableLoadMoreIfNotFullPage()
            preLoadNumber = 2
            setOnLoadMoreListener {
                getData(page.loadMore())
            }
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            WebActivity.start(
                requireContext(),
                "/mobile/Course/detail?id=${mAdapter.data[position].id}"
            )
        }
    }

    override fun initListener(view: View) {
        super.initListener(view)
        refreshLayout.setOnRefreshListener {
            getData(page.refresh())
        }
    }

    private fun getData(pageNum: Int) {
        mModel.getCashRecord(pageNum, mIndex).observe(this, Observer {
            when (it?.status) {
                Status.SUCCESS -> {
                    loadSuccess(it)
                }
                Status.ERROR -> {
                    page.fail()
                    refreshLayout.isRefreshing = false
                    mAdapter.loadMoreModule.loadMoreFail()
                    ToastUtils.showShort(it.message.getMsg())
                }
                Status.LOADING -> {
                    if (page.isRefresh() && !refreshLayout.isRefreshing) {
                        refreshLayout.isRefreshing = true
                    }
                }
            }
        })
    }

    private fun loadSuccess(it: Result<MutableList<CourseInfo>>) {
        val temp = it.requireData()
        if (page.isRefresh()) {
            mAdapter.setList(temp)
        } else {
            mAdapter.addData(temp)
        }
        page.success()
        if (temp.size < mModel.pageSize) {
            mAdapter.loadMoreModule.loadMoreEnd()
        } else {
            mAdapter.loadMoreModule.loadMoreComplete()
        }
        refreshLayout.isRefreshing = false
    }


    override fun initObserver() {
        super.initObserver()
        getData(page.refresh())
    }
}